
******************************
*** Create SOM survey data ***
******************************

*** SOM 2016 ***

/* dataset available after signing agreement with SOM institute */
use "originalSOM2016.dta", clear

* keep question on good/bad proposal to accept fewer refugees and media usage
keep f59b kommun 

* create string municipality
decode kommun, gen(municipality)

foreach i in 114 123 160 188 192 428 486 562 683 687 834 883 1261 1278 1293 1401 ///
1427 1439 1442 1444 1447 1482 1487 1488 1489 1490 1785 1860 1863 1980 2031 2081 2082 2121 2180 2183 2262 2283 ///
2305  2361 2418 2460  2480 2482 2523  2580 2581{
replace municipality=ustrfix(municipality, "a") if kommun==`i'
}

foreach i in 319 561 765 1277 1292 2039 2260 2463 2560 2321{
replace municipality=ustrfix(municipality, "A") if kommun==`i'
}

foreach i in 125 138 186 381 480 481 580 581 582 586 617 642 680 686 821 1060 1083 1214 1231  ///
 1265 1266 1272 1280 1285  1419 1471 1473  1480 1481 1486 1494 1496 1499 1761 1960 1983 2182 /// 
 2313  2417 {
replace municipality=ustrfix(municipality, "o") if kommun==`i'
}

foreach i in 382  1257 1880  2380 2513{
replace municipality=ustrfix(municipality, "O") if kommun==`i'
}

foreach i in 117 120 181 305 509 682 684 780 840 861 1256 1267 1284 1407 1473 1492 1765 2280 2284 2518 {
replace municipality=ustrfix(municipality, "" ) if kommun==`i'
}

replace municipality="Osteraker" if municipality=="sterker"
replace municipality="Varmdo" if municipality=="Vrmd"
replace municipality="Haabo" if municipality=="Hbo"
replace municipality="Odeshog" if municipality=="deshg"
replace municipality="Nassjo" if municipality=="Nssj"
replace municipality="Savsjo" if municipality=="Svsj"
replace municipality="Vaxjo" if municipality=="Vxj"
replace municipality="Morbylanga" if municipality=="Mrbylnga"
replace municipality="Monsteras" if municipality=="Mnsters"
replace municipality="Hoor" if municipality=="Hr"
replace municipality="Hoganas" if municipality=="Hgans"
replace municipality="Ockero" if municipality=="cker"
replace municipality="Amal" if municipality=="ml"
replace municipality="Arjang" if municipality=="rjng"
replace municipality="Harnosand" if municipality=="Hrnsand"
replace municipality="Overtornea" if municipality=="vertorne"
replace municipality="Dals-ed" if municipality=="Dals-Ed"
replace municipality="Lilla edet" if municipality=="Lilla Edet"
replace municipality="Malung-salen" if municipality=="Malung"
replace municipality="Upplands-bro" if municipality=="Upplands-Bro"
replace municipality="Upplands-vasby" if municipality=="Upplands-Vasby"
replace municipality="Ornskoldsvik" if municipality=="rnskldsvik"
replace municipality="Ostra goinge" if municipality=="stra Ginge"
replace municipality="Sodertalje" if municipality=="Sdertlje"

* drop missing information
drop if f59b==97 | f59b==98 | f59b==99
drop if municipality=="Uppgift saknas"

* generate mean for opinion
collapse f59b, by(municipality)
rename f59b opinion16

save "SOM2016.dta", replace


** SOM 2015 **

/* dataset available after signing agreement with SOM institute */
use "originalSOM2015.dta", clear

* keep question on good/bad proposal to accept fewer refugees and media usage, age, gender, education, residence
keep f49b f4a f4b f4c f4d f4e f4f f4g f4h f4i f4j f7a f7b f7c f7d f7e f7f f7g ///
f7h f7i f7j f7k kommun ln f137 f138 f148 hushink

* create string municipality
decode kommun, gen(municipality)

foreach i in 114 123 160 188 192 428 486 562 683 687 834 883 1261 1278 1293 1401 ///
1427 1439 1442 1444 1447 1488 1489 1490 1785 1860 1863 1980 2031 2121 2180 2183 2262 2283 ///
2305  2361 2460  2480 2482 2523  2580 2581{
replace municipality=ustrfix(municipality, "a") if kommun==`i'
}

foreach i in 319 561 765 1277 1292 2039 2260 2463 2560 2321{
replace municipality=ustrfix(municipality, "A") if kommun==`i'
}

foreach i in 125 138 186 381 480 481 580 581 582 586 617 642 680 686 821 1060 1083 1214 1231  ///
 1265 1266 1272 1280 1285  1419 1471  1480 1481 1486 1494 1496 1499 1761 1960 1983 2182 /// 
 2313  2417 {
replace municipality=ustrfix(municipality, "o") if kommun==`i'
}

foreach i in 382  1257 1880  2380 2513{
replace municipality=ustrfix(municipality, "O") if kommun==`i'
}

foreach i in 117 120 181 305 509 682 684 780 840 861 1256 1267 1284 1407 1473 1492 1765 2280 2284 2518 {
replace municipality=ustrfix(municipality, "" ) if kommun==`i'
}

replace municipality="Osteraker" if municipality=="sterker"
replace municipality="Varmdo" if municipality=="Vrmd"
replace municipality="Haabo" if municipality=="Hbo"
replace municipality="Odeshog" if municipality=="deshg"
replace municipality="Nassjo" if municipality=="Nssj"
replace municipality="Savsjo" if municipality=="Svsj"
replace municipality="Vaxjo" if municipality=="Vxj"
replace municipality="Morbylanga" if municipality=="Mrbylnga"
replace municipality="Monsteras" if municipality=="Mnsters"
replace municipality="Hoor" if municipality=="Hr"
replace municipality="Hoganas" if municipality=="Hgans"
replace municipality="Ockero" if municipality=="cker"
replace municipality="Amal" if municipality=="ml"
replace municipality="Arjang" if municipality=="rjng"
replace municipality="Harnosand" if municipality=="Hrnsand"
replace municipality="Overtornea" if municipality=="vertorne"
replace municipality="Borlange" if municipality=="Borlänge"
replace municipality="Sater" if municipality=="Säter"
replace municipality="Dals-ed" if municipality=="Dals-Ed"
replace municipality="Kungalv" if municipality=="Kungälv"
replace municipality="Lilla edet" if municipality=="Lilla Edet"
replace municipality="Malung-salen" if municipality=="Malung"
replace municipality="Vanersborg" if municipality=="Vänersborg"
replace municipality="Upplands-bro" if municipality=="Upplands-Bro"
replace municipality="Upplands-vasby" if municipality=="Upplands-Vasby"
replace municipality="Mala" if municipality=="Malå"
replace municipality="Ornskoldsvik" if municipality=="rnskldsvik"
replace municipality="Ostra goinge" if municipality=="stra Ginge"
replace municipality="Toreboda" if municipality=="Teboda"
replace municipality="Sodertalje" if municipality=="Sdertlje"

* create string for county
decode ln, gen(county)

replace county=ustrfix(county, "" ) 

replace county="Skane" if county=="Skne ln"
replace county="Kalmar" if county=="Kalmar ln"
replace county="Stockholm" if county=="Stockholms ln"
replace county="Östergötland" if county=="stergtlands ln"
replace county="Södermanland" if county=="Sdermanlands ln"
replace county="Halland" if county=="Hallands ln"
replace county="Kronoberg" if county=="Kronobergs ln"
replace county="Blekinge" if county=="Blekinge ln"
replace county="Västra Götaland" if county=="Vstra Gtalands ln"
replace county="Jönköping" if county=="Jnkpings ln"
replace county="Gotland" if county=="Gotlands ln"
replace county="Västmanland" if county=="Vstmanlands ln"
replace county="Uppsala" if county=="Uppsala ln"
replace county="Gävleborg" if county=="Gvleborgs ln"
replace county="Jämtland" if county=="Jmtlands ln"
replace county="Västnorrland" if county=="Vsternorrlands ln"
replace county="Dalarna" if county=="Dalarnas l ln"
replace county="Örebro" if county=="rebro ln"
replace county="Västerbotten" if county=="Vsterbottens ln"
replace county="Värmland" if county=="Vrmlands ln"
replace county="Norrbotten" if county=="Norrbottens ln"

* create dummies for often or seldom use of the several news sources
foreach i in a b c d e f g h {
gen often`i'=1 if f4`i'==1| f4`i'==2 |f4`i'==3 | f4`i'==4
replace often`i'=0 if often`i'==.
}

foreach i in a b c d e f g h {
gen seldom`i'=1 if f4`i'==5| f4`i'==6 
replace seldom`i'=0 if seldom`i'==.
}

* create variable for gender
gen Gender="Female" if f137==1
replace Gender="Male" if f137==2

* create variable for age
gen Age="76-85" if f138>=1930 & f138<=1939
replace Age="66-75" if f138>=1940 & f138<=1949
replace Age="56-65" if f138>=1950 & f138<=1959
replace Age="46-55" if f138>=1960 & f138<=1969
replace Age="36-45" if f138>=1970 & f138<=1979
replace Age="26-35" if f138>=1980 & f138<=1989
replace Age="16-25" if f138>=1990 & f138<=1999

* create variable for education
gen Education=1 if f148==1 | f148==2
replace Education=2 if f148==3 | f148==4
replace Education=3 if f148==5 | f148==6
replace Education=4 if f148==7 | f148==8
label define educ 1 "Low" 2 "Medium-low" 3 "Medium-high" 4 "High"
label values Education educ

* create variable for income
rename hushink Income
label define inc 1 "Max 300,000 SEK" 2 "301,000-700,000 SEK" 3 "More than 700,000 SEK"
label values Income inc

* create dataset for DN use
save "SOMmedia.dta", replace

* create dummies for trust in DN
gen trust=3 if f7j==1 | f7j==2
replace trust=2 if f7j==3
replace trust=1 if f7j==4 | f7j==5

* create dataset for DN trust
preserve

collapse trust, by(municipality)
drop if municipality==""

save "SOMtrust.dta", replace

restore

* generate mean for opinion
collapse f49b, by(municipality)
rename f49b opinion15

drop if municipality==""

save "SOM2015.dta", replace



*** SOM 2014 ***

/* dataset available after signing agreement with SOM institute */
use "originalSOM2014.dta", clear

* keep question on good/bad proposal to accept fewer refugees
keep f43b kommun
keep if f43b!=. & kommun!=.

* change labels for special swedish characters in municipality names
label define levels 114 "Upplands-vasby" 115 "Vallentuna" 117 "Osteraker"  ///
120 "Varmdo" 123 "Jarfalla" 125 "Ekero" 126 "Huddinge" 127 "Botkyrka" 128 "Salem" ///
136 "Haninge" 138 "Tyreso" 139 "Upplands-bro" 140 "Nykvarn" 160 "Taby" 162 "Danderyd" ///
163 "Sollentuna" 180 "Stockholm" 181 "Sodertalje" 182 "Nacka" 183 "Sundbyberg" ///
184 "Solna" 186 "Lidingo" 187 "Vaxholm" 188 "Norrtalje" 191 "Sigtuna" ///
192 "Nynashamn" 305 "Haabo" 319 "Alvkarleby" 330 "Knivsta" 331 "Heby" 360 "Tierp" ///
380 "Uppsala" 381 "Enkoping" 382 "Osthammar" 428 "Vingaker" 461 "Gnesta" 480 "Nykoping" ///
481 "Oxelosund" 482 "Flen" 483 "Katrineholm" 484 "Eskilstuna" 486 "Strangnas" ///
488 "Trosa" 509 "Odeshog" 512 "Ydre" 513 "Kinda" 560 "Boxholm" 561 "Atvidaberg" ///
562 "Finspang" 563 "Valdemarsvik" 580 "Linkoping" 581 "Norrkoping" 582 "Soderkoping" ///
583 "Motala" 584 "Vadstena" 586 "Mjolby" 04 "Aneby" 617 "Gnosjo" 642 "Mullsjo" 643 "Habo" 662 "Gislaved" 665 "Vaggeryd" 680 "Jonkoping" 682 "Nassjo"683 "Varnamo" ///
684 "Savsjo" ///
685 "Vetlanda" ///
686 "Eksjo" ///
687 "Tranas" ///
760 "Uppvidinge" ///
761 "Lessebo" ///
763 "Tingsryd" ///
764 "Alvesta" ///
765 "Almhult" ///
767 "Markaryd" ///
780 "Vaxjo" ///
781 "Ljungby" ///
821 "Hogsby" ///
834 "Torsas" ///
840 "Morbylanga" ///
860 "Hultsfred" ///
861 "Monsteras" ///
862 "Emmaboda" ///
880 "Kalmar" ///
881 "Nybro" /// 
882 "Oskarshamn" ///
883 "Vastervik" ///
884 "Vimmerby" ///
885 "Borgholm" ///
980 "Gotland" ///
1060 "Olofstrom" ///
1080 "Karlskrona" /// 
1081 "Ronneby" ///
1082 "Karlshamn" ///
1083 "Solvesborg" ///
1214 "Svalov" ///
1230 "Staffanstorp" ///
1231 "Burlov" ///
1233 "Vellinge" ///
1256 "Ostra goinge" ///
1257 "Orkelljunga" ///
1260 "Bjuv" ///
1261 "Kavlinge" ///
1262 "Lomma" ///
1263 "Svedala" ///
1264 "Skurup" ///
1265 "Sjobo" ///
1266 "Horby" ///
1267 "Hoor" ///
1270 "Tomelilla" ///
1272 "Bromolla" ///
1273 "Osby" ///
1275 "Perstorp" ///
1276 "Klippan" ///
1277 "Astorp" ///
1278 "Bastad" ///
1280 "Malmo" ///
1281 "Lund" ///
1282 "Landskrona" ///
1283 "Helsingborg" ///
1284 "Hoganas" ///
1285 "Eslov" ///
1286 "Ystad" /// 
1287 "Trelleborg" ///
1290 "Kristianstad" ///
1291 "Simrishamn" ///
1292 "Angelholm" ///
1293 "Hassleholm" ///
1315 "Hylte" ///
1380 "Halmstad" ///
1381 "Laholm" ///
1382 "Falkenberg" ///
1383 "Varberg" /// 
1384 "Kungsbacka" ///
1401 "Harryda" ///
1402 "Partille" ///
1407 "Ockero" ///
1415 "Stenungsund" ///
1419 "Tjorn" ///
1421 "Orust" ///
1427 "Sotenas" ///
1430 "Munkedal" ///
1435 "Tanum" ///
1438 "Dals-ed" ///
1439 "Fargelanda" ///
1440 "Ale" ///
1441 "Lerum" ///
1442 "Vargarda" ///
1443 "Bollebygd" ///
1444 "Grastorp" ///
1445 "Essunga" /// 
1446 "Karlsborg" ///
1447 "Gullspang" ///
1452 "Tranemo" ///
1460 "Bengtsfors" ///
1461 "Mellerud" ///
1462 "Lilla edet" ///
1463 "Mark" ///
1465 "Svenljunga"  ///
1466 "Herrljunga" ///
1470 "Vara" ///
1471 "Gotene" ///
1472 "Tibro" ///
1473 "Toreboda" ///
1480 "Goteborg" ///
1481 "Molndal" ///
1482 "Kungalv" ///
1484 "Lysekil" ///
1485 "Uddevalla" ///
1486 "Stromstad" ///
1487 "Vanersborg" ///
1488 "Trollhattan" ///
1489 "Alingsas" ///
1490 "Boras" ///
1491 "Ulricehamn" ///
1492 "Amal" ///
1493 "Mariestad" ///
1494 "Lidkoping" ///
1495 "Skara" ///
1496 "Skovde" ///
1497 "Hjo" ///
1498 "Tidaholm" ///
1499 "Falkoping" ///
1715 "Kil" ///
1730 "Eda" ///
1737 "Torsby" ///
1760 "Storfors" ///
1761 "Hammaro" ///
1762 "Munkfors" ///
1763 "Forshaga" ///
1764 "Grums" ///
1765 "Arjang" ///
1766 "Sunne" ///
1780 "Karlstad" ///
1781 "Kristinehamn" ///
1782 "Filipstad" ///
1783 "Hagfors" ///
1784 "Arvika" ///
1785 "Saffle" ///
1814 "Lekeberg" /// 
1860 "Laxa" ///
1861 "Hallsberg" ///
1862 "Degerfors" ///
1863 "Hallefors" ///
1864 "Ljusnarsberg" ///
1880 "Orebro" /// 
1881 "Kumla" ///
1882 "Askersund" ///
1883 "Karlskoga" ///
1884 "Nora" ///
1885 "Lindesberg" ///
1904 "Skinnskatteberg" ///
1907 "Surahammar" ///
1960 "Kungsor" ///
1961 "Hallstahammar" ///
1962 "Norberg" ///
1980 "Vasteras" ///
1981 "Sala" ///
1982 "Fagersta" ///
1983 "Koping" ///
1984 "Arboga" ///
2021 "Vansbro" ///
2023 "Malung-salen" ///
2026 "Gagnef" ///
2029 "Leksand" ///
2031 "Rattvik" ///
2034 "Orsa" ///
2039 "Alvdalen" ///
2061 "Smedjebacken" ///
2062 "Mora" ///
2080 "Falun" ///
2081 "Borlange" ///
2082 "Sater" ///
2083 "Hedemora" ///
2084 "Avesta" ///
2085 "Ludvika" ///
2101 "Ockelbo" /// 
2104 "Hofors" ///
2121 "Ovanaker" ///
2132 "Nordanstig" ///
2161 "Ljusdal" ///
2180 "Gavle" ///
2181 "Sandviken" ///
2182 "Soderhamn" ///
2183 "Bollnas" ///
2184 "Hudiksvall" ///
2260 "Ange" ///
2262 "Timra" ///
2280 "Harnosand" ///
2281 "Sundsvall" /// 
2282 "Kramfors" ///
2283 "Solleftea" ///
2284 "Ornskoldsvik" ///
2303 "Ragunda" ///
2305 "Bracke" ///
2309 "Krokom" ///
2313 "Stromsund" ///
2321 "Are" /// 
2326 "Berg" ///
2361 "Harjedalen" ///
2380 "Ostersund" ///
2401 "Nordmaling" /// 
2403 "Bjurholm" ///
2404 "Vindeln" ///
2409 "Robertsfors" ///
2417 "Norsjo" ///
2418 "Mala" ///
2421 "Storuman" ///
2422 "Sorsele" ///
2425 "Dorotea" ///
2460 "Vannas" ///
2462 "Vilhelmina" ///
2463 "Asele" ///
2480 "Umea" ///
2481 "Lycksele" ///
2482 "Skelleftea" ///
2505 "Arvidsjaur" ///
2506 "Arjeplog" ///
2510 "Jokkmokk" ///
2513 "Overkalix" ///
2514 "Kalix" ///
2518 "Overtornea" ///
2521 "Pajala" ///
2523 "Gallivare" ///
2560 "Alvsbyn" /// 
2580 "Lulea" ///
2581 "Pitea" ///
2582 "Boden" ///
2583 "Haparanda" ///
2584 "Kiruna" ///


label values kommun levels

decode kommun, gen(municipality)
decode ln, gen(county)

replace county=ustrfix(county, "" ) 

replace county="Skane" if county=="Skne ln"
replace county="Kalmar" if county=="Kalmar ln"
replace county="Stockholm" if county=="Stockholms ln"
replace county="Östergötland" if county=="stergtlands ln"
replace county="Södermanland" if county=="Sdermanlands ln"
replace county="Halland" if county=="Hallands ln"
replace county="Kronoberg" if county=="Kronobergs ln"
replace county="Blekinge" if county=="Blekinge ln"
replace county="Västra Götaland" if county=="Vstra Gtalands ln"
replace county="Jönköping" if county=="Jnkpings ln"
replace county="Gotland" if county=="Gotlands ln"
replace county="Västmanland" if county=="Vstmanlands ln"
replace county="Uppsala" if county=="Uppsala ln"
replace county="Gävleborg" if county=="Gvleborgs ln"
replace county="Jämtland" if county=="Jmtlands ln"
replace county="Västnorrland" if county=="Vsternorrlands ln"
replace county="Dalarna" if county=="Dalarnas ln"
replace county="Örebro" if county=="rebro ln"
replace county="Västerbotten" if county=="Vsterbottens ln"
replace county="Värmland" if county=="Vrmlands ln"
replace county="Norrbotten" if county=="Norrbottens ln"


* create dummy for opinion: 1 if good/rather good proposal
gen opinion=1 if f43b==1 | f43b==2
replace opinion=2 if f43b==3
replace opinion=3 if f43b==4 | f43b==5
/* 1 and 2 are bra, 3 is indifferent, 4 and 5 are dalig*/

* number of positive and negative opinions by municipality
bysort municipality: egen yes=count(opinion) if opinion==1
bysort municipality: egen no=count(opinion) if opinion==3
bysort municipality: egen ind=count(opinion) if opinion==2

collapse yes no ind, by(municipality)

* categorical variable for opinion: 3 good proposal, 2 indifferent, 1 bad
gen try=3 if yes>no & yes>ind
replace try=2 if ind>yes & ind>no
replace try=1 if no>yes & no>ind
rename try opinion

replace opinion=2 if yes==no
replace opinion=3 if yes>no & yes==ind
replace opinion=1 if no>yes & no==ind

save "SOM2014.dta", replace







